package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProductDaoImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        return 0;
    }

    @Override
    public int update(ProductModel productModel) {
        return 0;
    }

    @Override
    public int delete(ProductModel productModel) {
        return 0;
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "   select *,c.name cuisine_name from product p\n" +
                "    inner join cuisine c\n" +
                "    on p.cuisine_id=c.id order by p.id desc;";
        ResultSet resultSet = JDBC.query(sql, new ArrayList());
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineName(resultSet.getString("cuisine_name"));
                model.setPrice(resultSet.getBigDecimal("price"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setIsRecommend(resultSet.getInt("is_recommend"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCreatTime(resultSet.getString("create_time"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public ProductModel findById(ProductModel productModel) {
        return null;
    }

    @Override
    public int getCount(ProductModel productModel) {
        String sql = "   select count(*) count from product p\n" +
                "    inner join cuisine c\n" +
                "    on p.cuisine_id=c.id   order by p.id desc;";
//        List listSql = new ArrayList();
//        SqlModel sqlModel = getSql(userModel);
//        Map map = getSql(userModel);
//        list和map的区别？
        ResultSet resultSet = JDBC.query(sql, new ArrayList());
        try {
            while (resultSet.next()) {
                return resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int changeEnable(ProductModel productModel) {
        return 0;
    }
}
